//
//  HomeHeader.m
//  预购清单
//
//  Created by  on 2022/10/16.
//

#import "HomeHeader.h"
@interface HomeHeader()
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (strong, nonatomic) IBOutlet UIView *cycle;
@property (strong, nonatomic) IBOutlet UILabel *Prepay;
@property (strong, nonatomic) IBOutlet UILabel *percent;
@property (strong, nonatomic) IBOutlet UILabel *payed;
@property (nonatomic, strong) CAShapeLayer *progressLayer;
@property (strong, nonatomic) IBOutlet UIButton *btn;
@property (nonatomic, strong) CAShapeLayer *bgLayer;

@end
@implementation HomeHeader
- (void)awakeFromNib{
    [super awakeFromNib];
    [super awakeFromNib];
    self.bgView.layer.masksToBounds = NO;
    self.bgView.layer.shadowColor = [@"#9B9B9B" color:0.2].CGColor;
    self.bgView.layer.shadowOffset = CGSizeMake(0, 2);
    self.bgView.layer.shadowRadius = 20;
    self.bgView.layer.cornerRadius = 20;
    self.bgView.layer.shadowOpacity = 1;
    self.bgLayer.strokeEnd = 1;
}
- (void)setModel:(Budget *)model{
    _model = model;
    self.Prepay.text = [NSString stringWithFormat:@"%@%0.2f",LS(@"¥"),model.waitPayed];
    self.payed.text = [NSString stringWithFormat:@"%@%0.2f",LS(@"¥"),model.payed];
    self.progressLayer.strokeEnd = model.total == 0 ? 0 : (model.payed + model.waitPayed) * 1.0 / model.total;
    self.percent.text = [NSString stringWithFormat:@"%0.2f%%",(self.progressLayer.strokeEnd * 100)];
    NSString *title = [NSString stringWithFormat:@"%@-%02d",model.year,[model.month intValue]];
    [self.btn setTitle:title forState:UIControlStateNormal];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (self.block) {
        self.block();
    }
}
- (CAShapeLayer *)progressLayer{
    if (!_progressLayer) {
        _progressLayer = [self getlayer:[@"#0041C4" color]];
        [self.cycle.layer addSublayer:_progressLayer];
    }
    return _progressLayer;
}
-(CAShapeLayer *)bgLayer{
    if (!_bgLayer) {
        _bgLayer = [self getlayer:[@"#0041C4" color:0.2]];
        _bgLayer.strokeEnd = 1;
        [self.cycle.layer addSublayer:_bgLayer];
    }
    return _bgLayer;
}
- (CAShapeLayer *)getlayer:(UIColor *)color{
    CAShapeLayer *layer = [CAShapeLayer layer];
    CGFloat r = self.cycle.bounds.size.width / 2;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(r, r) radius:r-8  startAngle:M_PI*3/2 endAngle:M_PI*7/2 clockwise:YES];
    layer.frame = self.cycle.bounds;
    layer.lineWidth = 16;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = color.CGColor;
    layer.path = path.CGPath;
    layer.strokeEnd = 0;
    layer.lineCap = kCALineJoinRound;
    return layer;
}
@end
